﻿using DevExpress.XtraReports.UI;
using DevTeam;
using SpreadsheetGear;
using System;
using System.Data;
using System.Windows.Forms;

namespace KoliUstu
{
    public partial class MainForm : DevForm
    {
        public MainForm()
        {
            InitializeComponent();
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "Office 2013";
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (!Login.DoLogin())
                this.Close();


            btnOku.ItemClick += btnOku_ItemClick;
        }

        void btnOku_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (cmbCustomer.EditValue.ToStr())
            {
                case "HOLY FASHION":
                case "HOLY FASHION CANADA":
                    using (OpenFileDialog fdOpen = new OpenFileDialog())
                    {
                        DataTable dtKU = new DataTable("KU");
                        foreach (string col in "Sayfa BoxNr Of SEASON BRAND TKZ DIVISION NAME ARTICLE COLOUR ORDER PO".Split(" ".ToCharArray()))
                            dtKU.Columns.Add(col, typeof(string));

                        foreach (string col in "XS S M L XL XXL 35 36 37 38 39 40 41 42 43 44 45 46 Total".Split(" ".ToCharArray()))
                            dtKU.Columns.Add(col, typeof(int));

                        fdOpen.Filter = "Excel Files (*.xls*;*.csv)|*.xls*;*.csv";
                        fdOpen.FilterIndex = 1;
                        fdOpen.Multiselect = false;
                        if (fdOpen.ShowDialog() == DialogResult.OK)
                        {
                            IWorkbook Workbook = SpreadsheetGear.Factory.GetWorkbook(fdOpen.FileName);
                            DataSet ds = Workbook.GetDataSet(SpreadsheetGear.Data.GetDataFlags.NoColumnHeaders);
                            foreach (DataTable dt in ds.Tables)
                                if (Workbook.Sheets[dt.TableName].Visible == SheetVisibility.Visible)
                                //if (dt.TableName.StartsWith("PL ") && dt.TableName.Length < 10)
                                {
                                    DataRowCollection rows = dt.Rows;

                                    int Season = 0, Size = 0, Total = 0;
                                    for (int i = 0; i < rows.Count; i++)
                                    {
                                        for (int c = 0; c < dt.Columns.Count; c++)
                                            switch (dt.Rows[i][c].ToString().Trim())
                                            {
                                                case "SEASON": Season = i; break;
                                                case "SIZE": Size = c; break;
                                                case "total quant.": Total = c; break;
                                            }

                                        if (Season > 0 && Size > 0 && Total > 0)
                                            break;
                                    }

                                    //bool SML = cmbDagilim.EditValue.ToString() == "SML";
                                    int arabeden = 0;
                                    for (int i = Season + 2; i < rows.Count; i++)
                                    {
                                        if (rows[i][2].ToString() == string.Empty)
                                            break;

                                        for (int x = 11; x <= 21; x += 2)
                                            arabeden += rows[i][x].ToInt();
                                    }
                                    bool SML = arabeden == 0;
                                    //Notify(SML ? "SML" : "37");

                                    for (int i = Season + 2; i < rows.Count; i++)
                                    {
                                        if (rows[i][2].ToString() == string.Empty)
                                            break;

                                        DataRow r = dtKU.NewRow();
                                        r["Sayfa"] = dt.TableName;
                                        r["BoxNr"] = rows[i][0];
                                        r["Of"] = rows[i][1];
                                        r["SEASON"] = rows[i][2];
                                        r["BRAND"] = rows[i][3];
                                        r["TKZ"] = rows[i][4];
                                        r["DIVISION"] = rows[i][5];
                                        r["NAME"] = rows[i][6];
                                        r["ARTICLE"] = rows[i][7];
                                        r["COLOUR"] = rows[i][8];
                                        r["ORDER"] = rows[i][9];
                                        r["PO"] = rows[i][10];
                                        r["Total"] = rows[i][23];
                                        if (SML)
                                        {
                                            r["XS"] = rows[i][12];
                                            r["S"] = rows[i][14];
                                            r["M"] = rows[i][16];
                                            r["L"] = rows[i][18];
                                            r["XL"] = rows[i][20];
                                            r["XXL"] = rows[i][22];
                                        }
                                        else for (int s = 35; s <= 46; s++)
                                                r[s.ToString()] = rows[i][11 + s - 35];

                                        dtKU.Rows.Add(r);

                                    }
                                }

                            string xml = dtKU.Serialize();
                            DataSet dsData = ExecuteDataSet("GetKoliUstuHolyFashion", xml);
                            KoliUstuHF hf = new KoliUstuHF(dsData, cmbCustomer.EditValue.ToStr().Contains("CANADA"));
                            hf.ShowPreviewDialog();
                        }
                    }
                    break;

                case "REISS":
                    using (OpenFileDialog fdOpen = new OpenFileDialog())
                    {
                        DataTable dtKU = new DataTable("KU");
                        foreach (string col in "Sayfa BoxNr Of ORDER STYLE ARTICLE COLOUR SIZE QUANTITY".Split(" ".ToCharArray()))
                            dtKU.Columns.Add(col, typeof(string));

                        foreach (string col in "XS S M L XL XXL".Split(" ".ToCharArray()))
                            dtKU.Columns.Add(col, typeof(int));

                        fdOpen.Filter = "Excel Files (*.xls*;*.csv)|*.xls*;*.csv";
                        fdOpen.FilterIndex = 1;
                        fdOpen.Multiselect = false;
                        if (fdOpen.ShowDialog() == DialogResult.OK)
                        {
                            IWorkbook Workbook = SpreadsheetGear.Factory.GetWorkbook(fdOpen.FileName);
                            DataSet ds = Workbook.GetDataSet(SpreadsheetGear.Data.GetDataFlags.NoColumnHeaders | SpreadsheetGear.Data.GetDataFlags.NoColumnTypes);
                            foreach (DataTable dt in ds.Tables)
                                if (dt.TableName.StartsWith("PL ")) // && dt.TableName.Length < 10
                                {
                                    DataRowCollection rows = dt.Rows;

                                    int BoxNumber = 0, Order = 0, Style = 0, Article = 0, Color = 0;
                                    bool DongulerdenCik = false;
                                    for (int i = 0; i < rows.Count; i++)
                                    {
                                        for (int c = 0; c < dt.Columns.Count; c++)
                                        {
                                            switch (dt.Rows[i][c].ToString().Trim())
                                            {
                                                case "BoxNumber": BoxNumber = i; break;
                                                case "OrderNr": Order = c; break;
                                                case "Style": Style = c; break;
                                                case "Article": Article = c; break;
                                                case "Color": Color = c; break;
                                            }

                                            if (BoxNumber > 0 && Order > 0 && Style > 0 && Article > 0 && Color > 0)
                                            {
                                                DongulerdenCik = true;
                                                break;
                                            }

                                        }
                                        if (DongulerdenCik)
                                            break;
                                    }


                                    for (int i = BoxNumber + 2; i < rows.Count; i++)
                                    {
                                        if (rows[i][1].ToString() == string.Empty)
                                            break;

                                        DataRow r = dtKU.NewRow();
                                        r["Sayfa"] = dt.TableName;
                                        r["BoxNr"] = rows[i][0];
                                        r["Of"] = rows[i][1];
                                        r["ORDER"] = rows[i][2];
                                        r["STYLE"] = rows[i][3];
                                        r["ARTICLE"] = rows[i][4];
                                        r["COLOUR"] = rows[i][5];

                                        r["XS"] = rows[i][7];
                                        r["S"] = rows[i][8];
                                        r["M"] = rows[i][9];
                                        r["L"] = rows[i][10];
                                        r["XL"] = rows[i][11];
                                        r["XXL"] = rows[i][12];

                                        String SizeStr = String.Empty,
                                                QuantityStr = String.Empty;

                                        for (int j = 7; j < 13; j++)
                                            if (rows[i][j].ToInt() > 0)
                                                switch (j)
                                                {
                                                    case 7: SizeStr += "XS - "; QuantityStr += rows[i][j].ToStr() + " - "; break;
                                                    case 8: SizeStr += "S - "; QuantityStr += rows[i][j].ToStr() + " - "; break;
                                                    case 9: SizeStr += "M - "; QuantityStr += rows[i][j].ToStr() + " - "; break;
                                                    case 10: SizeStr += "L - "; QuantityStr += rows[i][j].ToStr() + " - "; break;
                                                    case 11: SizeStr += "XL - "; QuantityStr += rows[i][j].ToStr() + " - "; break;
                                                    case 12: SizeStr += "XXL - "; QuantityStr += rows[i][j].ToStr() + " - "; break;
                                                }

                                        if (!SizeStr.Equals(String.Empty))
                                            SizeStr = SizeStr.Substring(0, SizeStr.Length - 3);

                                        if (!QuantityStr.Equals(String.Empty))
                                            QuantityStr = QuantityStr.Substring(0, QuantityStr.Length - 3);

                                        r["SIZE"] = SizeStr;
                                        r["QUANTITY"] = QuantityStr;

                                        dtKU.Rows.Add(r);

                                    }
                                }

                            string xml = dtKU.Serialize();
                            DataSet dsData = ExecuteDataSet("GetKoliUstuReiss", xml);
                            KoliUstuReiss hf = new KoliUstuReiss(dsData);
                            hf.ShowPreviewDialog();
                        }
                    }
                    break;

            }

        }

    }
}
